package com.ext.springcloud.producer.order;

import com.ext.springcloud.channel.order.OrderSetChannel;
import com.ext.springcloud.result.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @FileName: OrderSetProducer
 * @Author: <a href="dongann@aliyun.com">dongchang'an</a>.
 * @CreateTime: 2021/8/12 11:06
 * @Version: v1.0
 * @description:
 */
@Slf4j
@RestController
@Valid
@EnableBinding(value = {OrderSetChannel.class})
public class OrderSetProducer {

    @Resource
    private OrderSetChannel orderSetChannel;

    /**
     * 订单下单成功发送消息
     * @param orderMsg 消息信息
     */
    @PostMapping("/msgProducer/sendSetOrderMsg")
    public CommonResult sendSetOrderMsg(@RequestParam(value = "orderMsg") String orderMsg){
        Message<String> build = MessageBuilder.withPayload(orderMsg).build();
        try {
            orderSetChannel.setOrderOutput().send(build);
            return new CommonResult(200, "success");
        }catch (Exception e){
            log.error("订单下单成功发送消息异常",e);
            return new CommonResult(201, "fail");
        }
    }
}
